﻿<?php
/**
 * Edition Manager
 *
 * Sewiki is a experimental software for course
 * Software Engineering in HIT and it's devoloped
 * upon CodeIgniter Framework. More infomation of
 * the framework, please vist http://codeigniter.com/
 *
 * @package  : CodeIgniter
 * @category : controllers/editor
 * @author   : Oneplus <oneplus.lau@gmail.com>
 * @date     : 2011-06-14
 */
class EditionManager extends CI_Controller {
	/**
	 * The constructor
	 *
	 * @access : public
	 * @return : void
	 */
	function __construct() {
		parent::__construct();
		$this->load->database();
		$this->load->model('book');
		$this->load->model('chapter');
		$this->load->model('edition');
	}
	
	/**
	 * Method Create Blank Edition
	 *
	 * Main controller method for editor create blank
	 * edition task logic
	 *
	 * @access : public
	 * @param  : int, $cid
	 * @return : void
	 */
	public function createBlankEdition($cid) {
		// step i load view
		$chapter = $this->chapter->getChapter($cid);
		$this->load->view('editor/createBlankEditionForm', array('chapter' => $chapter));
		
		// step ii if the submit button is pressed store the infomation to database
		if($this->input->post('submit')) {
			$data = array(
				'uid'          => $this->session->userdata('uid'),
				'create_time'  => date("Y-m-d H:i:s", time()),
				'content'      => $this->input->post('content'),
				'comment'      => $this->input->post('comment'),
				'public_level' => $this->input->post('public_level'),
				'cid'          => $cid);
				
			$this->edition->createEdition($data);
			
			// step iii show some message
			$sessiondata = array('msg' => '新版本创建成功');
			$this->session->set_userdata($sessiondata);
			redirect('browser/Browser/editionList/' . strval($cid));
		}
	}
	
	/**
	 * Method Create History Edition
	 *
	 * Main controller method for editor create history
	 * edition task logic
	 *
	 * @access : public
	 * @param  : int, $cid
	 * @param  : int, $eid
	 * @return : void
	 */
	public function createHistoryEdition($cid, $eid) {
		// step i prepare edition information
		$chapter = $this->chapter->getChapter($cid);
		$historyEdition = $this->db->get_where('editions', array('eid' => $eid));
		$data = array(
			'chapter' => $chapter,
			'content' => $historyEdition->row()->content);
		
		// step ii load view
		$this->load->view('editor/createHistoryEditionForm', $data);
		
		// step ii if the submit button is pressed, update the infomation to database
		if($this->input->post('submit')) {
			$data = array(
				'uid'          => $this->session->userdata('uid'),
				'create_time'  => date("Y-m-d H:i:s", time()),
				'content'      => $this->input->post('content'),
				'comment'      => $this->input->post('comment'),
				'public_level' => $this->input->post('public_level'),
				'cid'          => $cid);
				
			$this->load->model('edition');
			$this->edition->createEdition($data);
			
			
			$sessiondata = array('msg' => '新版本创建成功');
			$this->session->set_userdata($sessiondata);
			redirect('browser/Browser/editionList/' . strval($cid));
		}
	}
}
?>